classes.sprites.Enemy = cc.PhysicsSprite.extend({
    move:false,
    direction:1,
    jumping:false,
    sprite:null,
    walking:false,
    walkAnim:5,
    spriteFrameIndex:0,
    hit:false,
    spriteFrameNamePrefix:"sWalk_left0",
    prevPrefix:"",
    jumpTimer:0,
    hitTimer:0,
    hitCount:0,
    deadTimer:0,
    dead: false,
    ctor:function (filename, body) {
        this._super();
        this.init();
        this.setBody(body);
        this.setScale(1.5);
        this.generateNewSprite(filename);
    },
    myUpdate:function (player) {
    	if(!this.hit && !this.dead && player.getPosition().x > this.getPosition().x + 20) {
    		this.direction = 3;
    		this.move = true;
    		this.spriteFrameNamePrefix = "sWalk_right";
    	} else if(!this.hit && !this.dead && player.getPosition().x < this.getPosition().x - 20) {
    		this.direction = -3;
    		this.move = true;
    		this.spriteFrameNamePrefix = "sWalk_left";
    	} else {
            this.move = false;
            if(this.spriteFrameNamePrefix === "sWalk_left")
                this.spriteFrameNamePrefix = "sStand_left";
            else
                this.spriteFrameNamePrefix = "sStand_right";
            this.generateNewSprite(this.spriteFrameNamePrefix + '.png');
            this.walkAnim = 0;
        }

        if(this.move) {
            this.walkAnim++;
            if(this.prevPrefix !== this.spriteFrameNamePrefix) {
                this.spriteFrameIndex = 0;
                this.generateNewSprite(this.spriteFrameNamePrefix + this.spriteFrameIndex + '.png');
            }
            if(this.walkAnim >= 8) {
                this.spriteFrameIndex++;
                if(this.spriteFrameIndex > 2)
                    this.spriteFrameIndex = 0;
                this.generateNewSprite(this.spriteFrameNamePrefix + this.spriteFrameIndex + '.png');
                this.walkAnim = 0;
            }
            this.prevPrefix = this.spriteFrameNamePrefix;
        }
        if(this.move) {
            this.setPosition(cc.p(this.getPosition().x+this.direction, this.getPosition().y));
        }
        if(this.hit) {
            this.move = false;
            if(this.hitCount==5) {
                this.dead = true;
                this.hit = false;
            }
            this.hitTimer++;
            if(this.hitTimer >= 45) {
                this.hit = false;
                this.hitTimer = 0;
                this.hitCount++;
            }
            if(this.direction > 0)
                this.spriteFrameNamePrefix = "sDuck_right";
            else
                this.spriteFrameNamePrefix = "sDuck_left";
            this.generateNewSprite(this.spriteFrameNamePrefix + '.png');
        }
        if(this.dead) {
            this.move = false;
            this.deadTimer++;
            if(this.direction > 0)
                this.spriteFrameNamePrefix = "sDead_right";
            else
                this.spriteFrameNamePrefix = "sDead_left";
            this.generateNewSprite(this.spriteFrameNamePrefix + '.png');
        }
        if(this.jumping) {
            this.jumpTimer++;
            if(this.jumpTimer >= 60) {
                this.jumpTimer = 0;
                this.jumping = false;
            }
        }
    },
    jump: function() {
        if(this.jumping) return;
        this.jumping = true;
        this.getBody().applyImpulse(cp.v(0,1500), cp.v(0,0));
    },
    generateNewSprite: function(name) {
        this.removeChild(this.sprite);
        this.sprite = cc.Sprite.createWithSpriteFrameName(name);
        this.addChild(this.sprite);
    }
});